package com.biejiaogroup.librarymanager.controller;

import com.biejiaogroup.librarymanager.entity.Book;
import com.biejiaogroup.librarymanager.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: JesseChan
 * @Date: 2022/5/5
 */
@WebServlet("/insertBook")
public class InsertServlet  extends HttpServlet {
    private BookService insertService = new BookService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String bookName = request.getParameter("bookName");
        String bookAuthor = request.getParameter("bookAuthor");
        Book book = new Book();
        book.setBookName(bookName);
        book.setBookAuthor(bookAuthor);

        boolean res = insertService.insertBook(book);
        PrintWriter out = response.getWriter();
        if(res)
        {
            out.print("success");
        }
        else
        {
            out.print("failed");
        }
    }
}
